// package com.ynet.middleground.mobile.approve.mq.consumer;
//
// import java.time.LocalDateTime;
// import java.util.Map;
// import java.util.Optional;
//
// import org.dozer.Mapper;
// import org.springframework.beans.factory.annotation.Autowired;
// import org.springframework.cloud.stream.annotation.StreamListener;
// import org.springframework.stereotype.Component;
// import org.springframework.transaction.annotation.Transactional;
//
// import com.ynet.core.common.ServiceResult;
// import com.ynet.core.exception.BusinessException;
// import com.ynet.core.log.IfpLogger;
// import com.ynet.middleground.approve.constant.OperatingTypeEnum;
// import com.ynet.middleground.mobile.approve.constant.MsgTypeEnum;
// import com.ynet.middleground.mobile.approve.dao.ReceiveOperateMqLogMapper;
// import com.ynet.middleground.mobile.approve.entity.ReceiveOperateMqLog;
// import com.ynet.middleground.mobile.approve.entity.TaskOperateStage;
// import com.ynet.middleground.mobile.approve.mq.SourceMobileApprove;
// import com.ynet.middleground.mobile.approve.service.ApproveCenterService;
// import com.ynet.middleground.mobile.approve.service.MessageService;
// import com.ynet.middleground.mobile.approve.service.TaskOperateStageService;
// import com.ynet.middleground.mobile.approve.utils.EnumUtil;
//
/// **
// * 项目名：ifp-mobile-approve <br>
// * 包名：com.ynet.middleground.mobile.approve.mq.consumer <br>
// * 类名：ChannelApproveStateConsumer <br>
// * 描述： 接收渠道端的审批处理结果，再真正调用审批中心对应处理服务
// *
// * <pre>
// * 保留备用，当前类无法满足 上一个审批节点不上移动审批，而下一个节点上移动审批处理的场景
// * </pre>
// *
// * @author Suremotoo
// * @create 2021-03-25 23:57
// */
// @Component
// public class ChannelApproveStateConsumer {
//
// @Autowired
// private TaskOperateStageService taskOperateStageService;
//
// @Autowired
// private ApproveCenterService approveCenterService;
//
// @Autowired
// private ReceiveOperateMqLogMapper receiveOperateMqLogMapper;
//
// @Autowired
// private Mapper mapper;
//
// @Autowired
// private MessageService messageService;
//
// /**
// * 渠道端通过本消息将审批执行前操作判断结果推送给移动审批 <br>
// * （核心系统主动推送指令结果，这里指需要长时间处理的指令或需要告知审批人指令执行结果的情况）
// *
// * @param msgMap 渠道端对审批处理的结果
// */
// @StreamListener(SourceMobileApprove.BLXD_TO_JKZTYDSP_QDSPJGXX)
// @Transactional(rollbackFor = Exception.class)
// public void inputApproveStateConsumer(Map<String, Object> msgMap) {
// IfpLogger.info("mq", "MQ 渠道端审批处理结果数据，从 Binding-{}收到信息-{}", SourceMobileApprove.BLXD_TO_JKZTYDSP_QDSPJGXX,
// msgMap);
// // 必填参数校验
// Object throwEx = null;
// try {
// throwEx =
// Optional.ofNullable(msgMap).orElseThrow(() -> new BusinessException("渠道审批结果推送数据不能为空", "ECMA0001"));
// throwEx = Optional.ofNullable(msgMap.get("traceId"))
// .orElseThrow(() -> new BusinessException("业务流水 traceId 不能为空", "ECMA0001"));
// throwEx = Optional.ofNullable(msgMap.get("prevTraceId"))
// .orElseThrow(() -> new BusinessException("审批操作执行前业务流水 prevTraceId 不能为空", "ECMA0001"));
// throwEx = Optional.ofNullable(msgMap.get("requestTime"))
// .orElseThrow(() -> new BusinessException("请求时间 requestTime 不能为空", "ECMA0001"));
// throwEx = Optional.ofNullable(msgMap.get("channel"))
// .orElseThrow(() -> new BusinessException("渠道不能为空", "ECMA0001"));
// throwEx = Optional.ofNullable(msgMap.get("hasCommand"))
// .orElseThrow(() -> new BusinessException("是否有执行指令 hasCommand 不能为空", "ECMA0001"));
// throwEx = Optional.ofNullable(msgMap.get("taskId"))
// .orElseThrow(() -> new BusinessException("任务 id taskId 不能为空", "ECMA0001"));
// throwEx = Optional.ofNullable(msgMap.get("allowApproveOperate"))
// .orElseThrow(() -> new BusinessException("是否允许当前审批操作 allowApproveOperate 不能为空", "ECMA0001"));
// } catch (BusinessException e) {
// IfpLogger.info("mq", "MQ 渠道端审批处理结果数据，消息校验出错！{}", throwEx);
// msgMap.put("errorMsg", e.getMessage());
// log(msgMap);
// return;
// }
//
// boolean hasCommand = (Boolean)msgMap.get("hasCommand");
// String taskId = String.valueOf(msgMap.get("taskId"));
// boolean allowApproveOperate = (Boolean)msgMap.get("allowApproveOperate");
// String reason = String.valueOf(msgMap.get("reason"));
// Object commandResultObj = msgMap.get("commandResult");
//
// String errorMsg = null;
// // 校验查询任务记录为空
// TaskOperateStage taskOperateStage = taskOperateStageService.getTaskOperateStageByTaskId(taskId);
// IfpLogger.info(Thread.currentThread().getStackTrace()[1].getMethodName(), "查询任务处理暂存表记录：{}", taskOperateStage);
// if (taskOperateStage == null) {
// msgMap.put("errorMsg", taskId + "该任务记录为空");
// log(msgMap);
// return;
// }
//
// MsgTypeEnum msgTypeEnum = null;
// if (allowApproveOperate) {
// OperatingTypeEnum operatingTypeEnum = EnumUtil.getEnumObject(OperatingTypeEnum.class,
// e -> e.getCode() == Integer.parseInt(taskOperateStage.getOperateType()), "操作类型参数值错误", "ECMA0001");
// ServiceResult serviceResult = null;
// // String msg = operatingTypeEnum.getDesc();
// switch (operatingTypeEnum) {
// // 同意
// case PASS:
// if (Boolean.TRUE.equals(taskOperateStage.getIsToBeSigned())) {
// // 如果查询有待签章，则不调用其他逻辑
// break;
// }
// String nextAssignee =
// taskOperateStageService.getNextAssigneeByOperateData(taskOperateStage.getBusinessOperateData());
// serviceResult =
// approveCenterService.invokeApproveCompleteTask(taskOperateStage, nextAssignee, null);
// msgTypeEnum = MsgTypeEnum.TODO;
// break;
// // 退回至发起人
// case RETURN:
// // serviceResult = approveCenterService.invokeApproveRecallBackToStartUserTask(taskOperateStage);
// // 退回也退回到 firstNode 节点
// serviceResult = approveCenterService.invokeApproveReject(taskOperateStage, operatingTypeEnum);
// msgTypeEnum = MsgTypeEnum.RETURN;
// break;
// // 驳回至发起人
// case REJECT_TO_STARTER_DISAGREE:
// case REJECT_TO_STARTER_MODIFICATION:
// serviceResult = approveCenterService.invokeApproveReject(taskOperateStage, operatingTypeEnum);
// msgTypeEnum = MsgTypeEnum.REJECT;
// break;
// default:
// break;
// }
//
// taskOperateStage.setIsCompleted(true);
// taskOperateStage.setIsAllow(true);
// taskOperateStage.setReason(reason);
// taskOperateStageService.updateById(taskOperateStage);
// errorMsg = messageService.pushApproveAfterMsgQueueAndSendDingTalkMsg(serviceResult, taskOperateStage,
// msgTypeEnum, operatingTypeEnum);
// } else {
// taskOperateStage.setGmtModified(LocalDateTime.now());
// taskOperateStage.setIsAllow(false);
// taskOperateStage.setReason(reason);
// taskOperateStageService.updateById(taskOperateStage);
// }
//
// if (hasCommand) {
// Integer assignee = taskOperateStage.getAssignee();
// IfpLogger.info(Thread.currentThread().getStackTrace()[1].getMethodName(), "给{}推送指令消息：{}", assignee, reason);
// MsgTypeEnum commandMsgTypeEnum =
// (commandResultObj != null && (Boolean)commandResultObj) ? MsgTypeEnum.PASSED : MsgTypeEnum.NOT_PASSED;
// messageService.sendDingtalkMessage(commandMsgTypeEnum, assignee, taskOperateStage.getProcessInstanceId(),
// taskId);
// }
// msgMap.put("errorMsg", errorMsg);
// // 记录 MQ 日志
// log(msgMap);
// }
//
// /**
// * MQ 日志
// *
// * @param msgMap
// */
// // @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
// public void log(Map<String, Object> msgMap) {
// String requestTime = String.valueOf(msgMap.get("requestTime"));
// msgMap.remove("requestTime");
// ReceiveOperateMqLog operateMqLog = mapper.map(msgMap, ReceiveOperateMqLog.class);
// operateMqLog.setGmtCreate(LocalDateTime.now());
// operateMqLog.setRequestTime(LocalDateTime.parse(requestTime));
// receiveOperateMqLogMapper.insert(operateMqLog);
// }
//
// }
